@page "/checkout"
@using BlazingPizza.Services
@inject OrderState OrderState
@inject HttpClient HttpClient
@inject NavigationManager NavigationManager
@implements IDisposable

<div class="main">
    @* <EditForm Model=Order.DeliveryAddress OnSubmit=CheckSubmission> *@
    <EditForm EditContext=editContext  OnValidSubmit=PlaceOrder>
        <div class="checkout-cols">
            <div class="checkout-order-details">
                <h4>Review order</h4>
                <OrderReview Order="Order" />
            </div>

            <div class="checkout-delivery-address">
                <h4>Deliver to...</h4>
                @if (isError)
                {
                    <div class="alert alert-danger">Please enter a name and address.</div>
                }
                <AddressEditor Address="Order.DeliveryAddress" />
            </div>
        </div>

        <button class="checkout-button btn btn-warning" type="Submit" disabled=@isError>
            Place order
        </button>
        @* <ValidationSummary /> *@
        <DataAnnotationsValidator />
    </EditForm>
</div>

@code {
    Order Order => OrderState.Order;
    bool isSubmitting;
    bool isError = false;
    private EditContext editContext;

    protected override void OnInitialized()
    {
        editContext = new(Order.DeliveryAddress);
        editContext.OnFieldChanged += HandleFieldChanged;
    }

    private void HandleFieldChanged(object sender, FieldChangedEventArgs e)
    {
        isError = !editContext.Validate();
        StateHasChanged();
    }

    public void Dispose()
    {
        editContext.OnFieldChanged -= HandleFieldChanged;
    }

    private async Task CheckSubmission(EditContext editContext)
    {
        isSubmitting = true;
        var model = editContext.Model as Address;
        isError = string.IsNullOrWhiteSpace(model?.Name)
        || string.IsNullOrWhiteSpace(model?.Line1)
        || string.IsNullOrWhiteSpace(model?.PostalCode);
        if (!isError)
        {
            await PlaceOrder();
        }
        isSubmitting = false;
    }

    async Task PlaceOrder()
    {
        var response = await HttpClient.PostAsJsonAsync(NavigationManager.BaseUri + "orders", OrderState.Order);
        var newOrderId = await response.Content.ReadFromJsonAsync<int>();
        OrderState.ResetOrder();
        NavigationManager.NavigateTo($"myorders/{newOrderId}");
    }

    protected void ShowError()
    {
        isError = true;
    }
}